package com.miniportal.base.controller;

import cn.hutool.http.HttpStatus;
import com.github.pagehelper.util.StringUtil;
import com.miniportal.base.convert.ContactPeopleConvert;
import com.miniportal.base.pojo.CommonResponse;
import com.miniportal.base.pojo.dto.ContactPeopleDto;
import com.miniportal.base.service.IContactPeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO
 *
 * @author zhouje
 * @since 2022/5/31 15:52
 */
@RestController
@RequestMapping("contactPeople")
public class ContactPeopleController {

    @Autowired
    private IContactPeopleService iContactPeopleService;

    @GetMapping("queryContactPeoples")
    private CommonResponse queryContactPeoples(Long contactId) {
        if(contactId==null) {
            return CommonResponse.builder().code(HttpStatus.HTTP_BAD_REQUEST).msg("归属人编号不能为空").build();
        }
        return CommonResponse.builder().code(HttpStatus.HTTP_OK).msg("查询成功").data(iContactPeopleService.queryContactPeoples(contactId)).build();
    }

    @PostMapping("adddOrUpdate")
    private CommonResponse addOrUpdate(@Validated ContactPeopleDto request){
        if(request.getContactId()!=null) {
            return CommonResponse.builder().code(HttpStatus.HTTP_OK).msg("更新操作成功").data(iContactPeopleService.updateById(ContactPeopleConvert.INSTANCE.dtoToDo(request))).build();
        }else {
            return CommonResponse.builder().code(HttpStatus.HTTP_OK).msg("新增操作成功").data(iContactPeopleService.save(ContactPeopleConvert.INSTANCE.dtoToDo(request))).build();
        }
    }

}
